package com.cx.reactor.pubsub;


import lombok.SneakyThrows;

import java.util.concurrent.Flow;

/**
 * @Author:cxy
 * @Date:2022/3/5 20:06
 */
public class PrintSubscriber implements Flow.Subscriber<Integer> {

    private Flow.Subscription subscription;
    int i;

    @Override
    public void onSubscribe(Flow.Subscription s) {
        this.subscription = s;
        subscription.request(2);
    }

    @SneakyThrows
    @Override
    public void onNext(Integer integer) {
        System.out.println("Recive onNext item: "+integer);
        subscription.request(2);
        if(i == 0){
            Thread.sleep(1000);
            i++;
        }
    }

    @Override
    public void onError(Throwable t) {
        System.out.println("PrintSubscriber onError :"+t.getMessage());
    }

    @Override
    public void onComplete() {
        System.out.println("PrintSubscriber onComplete");
    }
}
